

package CtrLayer;
import ModelLayer.*;
import DBLayer.*;
import java.util.ArrayList;
/**
 */
public class CtrCostumer {


    /** Creates a new instance of CtrCostumer */
    public CtrCostumer() {

    }
    public ArrayList findAllCustomers()
    {
      IFDBCustomer dbCos = new DBCostumer();
      ArrayList allCos = new ArrayList();
      allCos = dbCos.getAllCostumers(false);
      return allCos;
    }
    public Costumer findByName(String fname)
    {
        IFDBCustomer dbCos = new DBCostumer();
        return dbCos.searchCostumerName(fname, true);
    }

      public Costumer findById(String ssn)
    {
        IFDBCustomer dbCos = new DBCostumer();
        return dbCos.findCostumer( ssn, true);
    }
      public int updateCostumer(int id, String name, String address, String zipCode, String phone)
      {
	  int res;
         System.out.println("update customer");
          IFDBCustomer dbCos = new DBCostumer();
          Costumer cos = new Costumer();
//	  CtrCityZip ctrCZ = new CtrCityZip();
//	  CityZip cz = ctrCZ.findCityZipByZip(zipCode);

          try {
	      cos.setId(id);
//	      cos.setCityZipId(cz.getId());
	      cos.setName(name);
	      cos.setAddress(address);
	      //cos.setCityZipId(cityZipId);
	      //cos.setCity(city);
	      //cos.setZipCode(zipCode);
	      cos.setPhoneno(phone);

	      
	      res = dbCos.updateCostumer(cos);
	  }
	  catch (NullPointerException e) {
	      res = 1;
	  }
          return res;


      }


      public int insertNew(String name, String address, String zipCode, String phone)
      {
	  System.out.println("insert customer");
          int res = 0;
           IFDBCustomer dbCos = new DBCostumer();
//	   CtrCityZip ctrCZ = new CtrCityZip();
//	   CityZip cz = ctrCZ.findCityZipByZip(zipCode);
	   try {
		Costumer cosObj = new Costumer();
		cosObj.setName(name);
		cosObj.setAddress(address);
		//cosObj.setCityZipId(cityZipId);
		//cosObj.setCity(city);
		cosObj.setZipCode("9000");
//		cosObj.setCityZipId(cz.getId());
		cosObj.setPhoneno(phone);
                System.out.println("text");
		dbCos.insertCostumer(cosObj);
	   }
	   catch (NullPointerException e) {
	       res = 1;
	   }
	   finally {
	       return res;
	   }
      }

    public int deleteCustomer(int id) {
	IFDBCustomer dbCos = new DBCostumer();
	return dbCos.delete(id);
    }




}
